#!/usr/bin/env python
"""
PyLink-SR (Python Linked Software Radio System)
(c) Bob Cowdery (G3UKB) 2008

File: jack_connect.py
Date: July 2008

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

The hw service.

"""

#-----------------------------------------------------------------------
# System imports
import logging
import sys
import os
import subprocess
from time import sleep
import Pyro.core
import Pyro.naming
from Pyro.EventService.Clients import Subscriber

# Set the PYTHONPATH
# This is a repeat of what goes on in app_main.py
HOME = os.environ['PYLINK_HOME']
sys.path.append(HOME + 'conf')
import config_service as config
config.open_config()
sys.path = sys.path + config.python_path().rsplit(',')

# Application imports
import defs
import config_service as config
import logger_service as logger
import sdr1k_controller

#-----------------------------------------------------------------------

class PylinkHwService(Subscriber):
    global glogger
    
    def __init__(self):
        Subscriber.__init__(self)
        self.setThreading(0)
        self.subscribe('hw_events')
        # Get the event service
        self.es = Pyro.core.getProxyForURI("PYRONAME://"+Pyro.constants.EVENTSERVER_NAME)
        # Initialise the controller. Note these values are stored in options.conf
        self.hw = None
        self.hw = sdr1k_controller.Sdr1kController(self.es)
    
    def event(self, event):
        # Wait for messages
        # Call the appropriate method        
        #print 'Received ', event.msg
        func, args = event.msg
        if func == 'abort':
            self.abort()
        else:
            bound_func = getattr(self.hw, func)
            bound_func(args)
    
#--------------------------------------------------------------------------
#
# Start from the command line
#
if __name__ == "__main__":
    logger.init_logger()
    glogger = logging.getLogger('pylink.logger')
    if config.mode() == 'standalone':
        Pyro.config.PYRO_HOST = config.pyro_local_host()
    else:
        Pyro.config.PYRO_HOST = config.pyro_remote_host()
    service = PylinkHwService()
    # Wait for messages
    glogger.info('PyLink-SR (HW Service) listening..')
    service.listen()
    glogger.info('PyLink-SR (HW Service) listener exiting..')
    glogger.info('PyLink-SR (HW Service) closing..')
    